package com.ss.progress.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ss.baseinfo.pojo.ClassInfo;
import com.ss.baseinfo.pojo.CourseInfo;
import com.ss.baseinfo.service.ClassService;
import com.ss.baseinfo.service.CourseService;
import com.ss.common.util.JsonUtil;
import com.ss.common.util.MyToolUtil;
import com.ss.progress.pojo.CalendarDetailInfo;
import com.ss.progress.pojo.CalendarInfo;
import com.ss.progress.service.CalendarService;

/**
 * 日历管理
 * @author Administrator
 * @date 2014.3.31
 */
@Controller
@RequestMapping("process/")
public class CalendarController {
	@Autowired
	private CalendarService service;//日历
	@Autowired
	private ClassService classService;//班级
	@Autowired
	private CourseService courseService;//课程
	/**
	 * 查询日历
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryCalendar.do")
	public ModelAndView queryCalendar(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		String course_name=MyToolUtil.convertNull(request.getParameter("course_name"));
		CalendarInfo info = new CalendarInfo();
		info.setCourse_name(course_name);
		List<CalendarInfo> list= service.queryCalendar(info);
		Map<String, Object> mapResult=new HashMap<String, Object>();
		mapResult.put("list",list);
		return new ModelAndView("/process/calendarManage",mapResult);
	}
	/**
	 * 保存日历数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveCalendar.do")
	public ModelAndView saveCalendar(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		String seq=MyToolUtil.convertNull(request.getParameter("seq"));
		String course_name=MyToolUtil.convertNull(request.getParameter("course_name"));
		String course_num=MyToolUtil.convertNull(request.getParameter("course_num"));
		String course_type=MyToolUtil.convertNull(request.getParameter("course_type"));
		String xz_class=MyToolUtil.convertNull(request.getParameter("xz_class"));
		String teacher_id=MyToolUtil.convertNull(request.getParameter("teacher"));
		String fact=MyToolUtil.convertNull(request.getParameter("fact"));
		String upper_limit=MyToolUtil.convertNull(request.getParameter("upper_limit"));
		String class_hour=MyToolUtil.convertNull(request.getParameter("class_hour"));
		String credit_hour=MyToolUtil.convertNull(request.getParameter("credit_hour"));
		String week=MyToolUtil.convertNull(request.getParameter("week"));
		String check_type=MyToolUtil.convertNull(request.getParameter("check_type"));
		String task_type=MyToolUtil.convertNull(request.getParameter("task_type"));
		
		CalendarInfo info = new CalendarInfo();
		info.setCheck_type(check_type);
		info.setClass_hour(class_hour);
		info.setCourse_name(course_name);
		info.setCourse_num(course_num);
		info.setCourse_type(course_type);
		info.setCredit_hour(credit_hour);
		info.setFact(fact);
		info.setSeq(seq);
		info.setTask_type(task_type);
		info.setTeacher_id(teacher_id);
		info.setUpper_limit(upper_limit);
		info.setWeek(week);
		info.setXz_class(xz_class);
		String msg="";
		//新增
		if("add".equals(action)){
			service.addCalendar(info);
			msg="保存成功";
		}else{
			boolean ret=service.updateCalendar(info);
			msg=ret==true?"保存成功":"修改失败";
		}
		Map<String, Object> mapResult=new HashMap<String, Object>();
		mapResult.put("msg",msg);
		return new ModelAndView("/process/calendarEdit",mapResult);
	}
	/**
	 * 跳转到日历编辑界面
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toEditCalendar.do")
	public ModelAndView toEditCalendar(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		//课程列表,班级列表
		List<ClassInfo> listClass=classService.query(null);
		List<CourseInfo> listCourse=courseService.query(null);
		mapResult.put("listClass", listClass);
		mapResult.put("listCourse", listCourse);
		return new ModelAndView("/process/calendarEdit",mapResult);
	}
	/**
	 * 保存日历明细数据
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveCalendarDetail.do")
	public void saveCalendarDetail(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		String calendar_id=MyToolUtil.convertNull(request.getParameter("calendar_id"));
		String week=MyToolUtil.convertNull(request.getParameter("week"));
		String course_content=MyToolUtil.convertNull(request.getParameter("course_content"));
		String course_kt_hour=MyToolUtil.convertNull(request.getParameter("course_kt_hour"));
		String course_kw_hour=MyToolUtil.convertNull(request.getParameter("course_kw_hour"));
		String test_content=MyToolUtil.convertNull(request.getParameter("test_content"));
		String test_kt_hour=MyToolUtil.convertNull(request.getParameter("test_kt_hour"));
		String test_kw_hour=MyToolUtil.convertNull(request.getParameter("test_kw_hour"));
		String lab_content=MyToolUtil.convertNull(request.getParameter("lab_content"));
		String lab_kt_hour=MyToolUtil.convertNull(request.getParameter("lab_kt_hour"));
		String lab_kw_hour=MyToolUtil.convertNull(request.getParameter("lab_kw_hour"));
		String books=MyToolUtil.convertNull(request.getParameter("books"));
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		//非空学时处理
		course_kt_hour=course_kt_hour.equals("")?"0":course_kt_hour;
		course_kw_hour=course_kw_hour.equals("")?"0":course_kw_hour;
		test_kt_hour=test_kt_hour.equals("")?"0":test_kt_hour;
		test_kw_hour=test_kw_hour.equals("")?"0":test_kw_hour;
		lab_kt_hour=lab_kt_hour.equals("")?"0":lab_kt_hour;
		lab_kw_hour=lab_kw_hour.equals("")?"0":lab_kw_hour;
		
		CalendarDetailInfo info = new CalendarDetailInfo();
		info.setBooks(books);
		info.setCalendar_id(calendar_id);
		info.setCourse_content(course_content);
		info.setCourse_kt_hour(course_kt_hour);
		info.setCourse_kw_hour(course_kw_hour);
		info.setTest_content(test_content);
		info.setTest_kt_hour(test_kt_hour);
		info.setTest_kw_hour(test_kw_hour);
		info.setLab_content(lab_content);
		info.setLab_kt_hour(lab_kt_hour);
		info.setLab_kw_hour(lab_kw_hour);
		info.setWeek(week);
		String msg="";
		//新增
		if("add".equals(action)){
			service.addCalendarDetail(info);
			msg="保存成功";
		}else{
			info.setId(id);
			boolean ret=service.updateCalendarDetail(info);
			msg=ret==true?"保存成功":"修改失败";
		}
		Map<String, Object> mapResult=new HashMap<String, Object>();
		mapResult.put("msg",msg);
		JsonUtil.outputJson(mapResult, response);
	}
	/**
	 * 查询日历明细
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryCalendarDetail.do")
	public ModelAndView queryCalendarDetail(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		CalendarDetailInfo info = new CalendarDetailInfo();
		String calendar_id=MyToolUtil.convertNull(request.getParameter("calendar_id"));
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		info.setCalendar_id(calendar_id);
		List<CalendarDetailInfo> list= service.queryCalendarDetail(info);
		Map<String, Object> mapResult=new HashMap<String, Object>();
		mapResult.put("list",list);
		if("print".equals(action)){
			return new ModelAndView("/process/calendarDetailPrint",mapResult);
		}else{
			return new ModelAndView("/process/calendarDetailManage",mapResult);
		}
	}
	
	/**
	 * 查询日历明细信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toEditCalendarDetail.do")
	public ModelAndView toEditCalendarDetail(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		CalendarDetailInfo info = new CalendarDetailInfo();
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		Map<String, Object> mapResult=new HashMap<String, Object>();
		if(id.equals("")){
			info=service.getCalendarDetailById(id);
			mapResult.put("info",info);
		}
		//课程列表,班级列表
		List<ClassInfo> listClass=classService.query(null);
		List<CourseInfo> listCourse=courseService.query(null);
		mapResult.put("listClass", listClass);
		mapResult.put("listCourse", listCourse);
		return new ModelAndView("/process/calendarDetailEdit",mapResult);
	}
	
}
